package com.becca.cake.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.model.system.RoleDO;
import com.becca.cake.service.system.IUserRoleService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.StringUtil;

/**
 * 角色管理
 * @author hejinguo
 * @version $Id: UserRoleAction.java, v 0.1 2013-12-2 下午10:58:11
 */
@Controller
@RequestMapping("/systemRole")
public class UserRoleAction extends BaseAction {
    private final static Log logger = LogFactory.getLog(UserRoleAction.class);
    @Autowired
    private IUserRoleService iUserRoleService;

    /**
     * 查询所有的角色
     * @author hejinguo
     * @return
     */
    @RequestMapping(value = "/selectAllRoles", method = RequestMethod.POST)
    public @ResponseBody
    String selectAllRoles() {
        try {
            List<RoleDO> listRoles = this.iUserRoleService.selectAllRoles();
            return JsonUtil.writeObjectNoPageJSON(listRoles);
        } catch (Exception e) {
            logger.error("查询所有角色失败!", e);
        }
        return JsonUtil.returnJsonUnSuccess("查询所有角色失败!");
    }

    /**
     * 角色管理中查询角色信息
     * @author hejinguo
     * @param roleDO
     * @return
     */
    @RequestMapping(value = "/selectAllRoleMessage", method = RequestMethod.POST)
    public @ResponseBody
    String selectAllRoleMessage(@ModelAttribute RoleDO roleDO) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleDO", roleDO);
        map.put("pagingBean", page);
        try {
            List<RoleDO> listRo = this.iUserRoleService.selectAllRoleMessage(map);
            Long count = this.iUserRoleService.selectAllRoleMessageCount(map);
            return JsonUtil.writeObjectPageJSON(listRo, count);
        } catch (Exception e) {
            logger.error("角色管理中查询角色信息失败!", e);
            return JsonUtil.returnJsonUnSuccess("角色管理中查询角色信息失败!");
        }
    }

    /**
     * 根据ID查询角色信息
     * @author hejinguo
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/selectRoleById", method = RequestMethod.POST)
    public @ResponseBody
    String selectRoleById(@RequestParam(value = "roleId", required = false) String roleId) {
        if (StringUtil.isNotEmpty(roleId)) {
            try {
                return this.iUserRoleService.selectRoleById(roleId);
            } catch (Exception e) {
                logger.error("查询角色信息失败!", e);
                return JsonUtil.returnJsonUnSuccess("查询角色信息失败!");
            }
        } else {
            logger.error("根据ID查询角色信息时ID为空!");
            return JsonUtil.returnJsonUnSuccess("根据ID查询角色信息时ID为空!");
        }
    }

    /**
     * 添加或修改角色信息
     * @author hejinguo
     * @param roleDO
     * @return
     */
    @RequestMapping(value = "/insetOrUpdateRole", method = RequestMethod.POST)
    public @ResponseBody
    String insetOrUpdateRole(@ModelAttribute RoleDO roleDO) {
        if (roleDO != null) {
            try {
                roleDO.setCreatedMan(this.getUserSeesion().getTrueName());
                roleDO.setUpdatedMan(this.getUserSeesion().getTrueName());
                this.iUserRoleService.insetOrUpdateRole(roleDO);
                return JsonUtil.returnJsonSuccess("添加或修改角色信息成功!");
            } catch (Exception e) {
                logger.error("添加或修改角色信息失败!", e);
                return JsonUtil.returnJsonUnSuccess("添加或修改角色信息失败!");
            }
        } else {
            logger.error("添加或修改角色信息为空!");
            return JsonUtil.returnJsonUnSuccess("添加或修改角色信息为空!");
        }
    }

    /**
     * 根据id删除角色信息
     * @author hejinguo
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteRoleById", method = RequestMethod.POST)
    public @ResponseBody
    String deleteRoleById(@RequestParam(value = "ids", required = false) String ids) {
        if (StringUtil.isNotEmpty(ids)) {
            try {
                this.iUserRoleService.deleteRoleById(ids);
                return JsonUtil.returnJsonSuccess("删除角色成功!");
            } catch (Exception e) {
                logger.error("删除角色失败!", e);
                return JsonUtil.returnJsonUnSuccess("删除角色失败!");
            }
        } else {
            logger.error("角色管理中删除角色信息角色ID为空!");
            return JsonUtil.returnJsonUnSuccess("角色管理中删除角色信息角色ID为空!");
        }
    }

    /**
     * 根据员工ID查询角色信息
     * @author hejinguo
     * @param adminId
     * @return
     */
    @RequestMapping(value = "/selectRoleByAdminId", method = RequestMethod.POST)
    public @ResponseBody
    String selectRoleByAdminId(@RequestParam(value = "adminId", required = false) String adminId) {
        if (StringUtil.isNotEmpty(adminId)) {
            try {
                return this.iUserRoleService.selectRoleByAdminId(adminId);
            } catch (Exception e) {
                logger.error("根据员工ID查询角色信息失败!", e);
                return JsonUtil.returnJsonUnSuccess("根据员工ID查询角色信息失败!");
            }
        } else {
            logger.error("根据员工ID查询角色信息时息员工ID为空!");
            return JsonUtil.returnJsonUnSuccess("根据员工ID查询角色信息时息员工ID为空!");
        }
    }

    /**
     * 根据部门ID查询对应的角色信息
     * @author hejinguo
     * @return
     */
    @RequestMapping(value = "/selectRoleByDeptId", method = RequestMethod.POST)
    public @ResponseBody
    String selectRoleByDeptId(@RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "deptId", required = false) String deptId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("admDeptId", this.getUserSeesion().getDeptId());
        map.put("deptId", deptId);
        try {
            return this.iUserRoleService.selectRoleByDeptId(map);
        } catch (Exception e) {
            logger.error("根据部门ID查询角色信息失败!", e);
            return JsonUtil.returnJsonUnSuccess("根据部门ID查询角色信息失败!");
        }
    }

    /**
     * 分配角色
     * @author hejinguo
     * @param adminId
     * @param roles
     * @return
     */
    @RequestMapping(value = "/insertAdminRole", method = RequestMethod.POST)
    public @ResponseBody
    String insertAdminRole(@RequestParam(value = "adminId", required = false) String adminId,
                           @RequestParam(value = "roles", required = false) String roles) {
        if (StringUtil.isNotEmpty(adminId) && StringUtil.isNotEmpty(roles)) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("adminId", adminId);
            map.put("roles", roles.split("[,]"));
            map.put("createdMan", this.getUserSeesion().getTrueName());
            try {
                this.iUserRoleService.insertAdminRole(map);
                return JsonUtil.returnJsonSuccess("分配成功!");
            } catch (Exception e) {
                logger.error("分配角色信息失败!", e);
                return JsonUtil.returnJsonUnSuccess("分配角色信息失败!");
            }

        } else {
            logger.error("分配角色时员工ID或角色ID为空!");
            return JsonUtil.returnJsonUnSuccess("分配角色时员工ID或角色ID为空!");
        }
    }
}
